package com.ivo.security.authenticator;

import java.security.Principal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ivo.model.hr.Employee;
import com.ivo.security.config.SecurityConfig;

/**
 * A simple RoleMapper which maps directly between group names and role names.
 * 
 * If a user is a member of someGroup, the are in the 'someGroup' role. Users
 * can log in if they exist.
 */
// hibernate caller
public class UserRoleMapper implements RoleMapper
{

	public void init( Map params, SecurityConfig config ) {
	}

	public UserRoleMapper() {
	}

	/**
	 * Assume that roles == groups.
	 */
	public boolean hasRole( Principal user, HttpServletRequest request, String role ) {
		Employee ruser = (Employee) user;
		if( user == null )
			return false;
		if( ruser.getId().equals(role) ) {
			return true;
		}

		return false; // ((User) user).contains(role);
	}

	/**
	 * Users can login if they exist.
	 */
	public boolean canLogin( Principal user, HttpServletRequest request ) {
		return user != null;
	}
}
